package com.jc.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jc.vo.ApiConfig;
import com.jc.vo.PolicyCalculateVo;
import com.jc.vo.PolicyRequest;
import com.jc.vo.PolicyResponseVO;
import java.io.IOException;

public class PolicyUtils {

	private PolicyUtils() {

	}


	public static R create(PolicyRequest policy, ApiConfig config) throws IOException {
		ObjectMapper om = new ObjectMapper();
		om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		String response = HttpPostUtil.send(om.writeValueAsString(policy), config, false);
		R r = om.readValue(response, R.class);
		try {
			if (r.getSuccess()) {
				PolicyResponseVO result = om.convertValue(r.getContent(), PolicyResponseVO.class);
				r.setContent(result);
			}
		} catch (Exception e) {
			r.setSuccess(false);
			r.setCode(R.RCODE.SYS_ERROR.getCode());
			r.setMessage(R.RCODE.SYS_ERROR.getMessage());
			e.printStackTrace();
		}
		return r;
	}

	/**
	 * 查询保单
	 * @param policy
	 * @param config
	 * @return
	 * @throws IOException
	 */
	public static R query(PolicyRequest policy, ApiConfig config) throws IOException {
		ObjectMapper om = new ObjectMapper();
		om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		String response = HttpPostUtil.send(om.writeValueAsString(policy), config, false);
		R r = om.readValue(response, R.class);
		if (r.getSuccess()) {
			PolicyResponseVO result = om.convertValue(r.getContent(), PolicyResponseVO.class);
			r.setContent(result);
		}
		return r;
	}

//	public static R uploadFile(File file, ApiConfig config) throws IOException {
//		String responseStr = "";
//		CloseableHttpClient httpclient = HttpClients.createDefault();
//		try {
//			HttpPost httpPost = new HttpPost(config.getServiceURL());
//			MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,null, StandardCharsets.UTF_8);
//			multipartEntity.addPart("file", new FileBody(file));
//			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
//			String now = format.format(new Date());
//			String secretId = config.getSecretId();
//			String sign = config.getSign(now);
//			multipartEntity.addPart("secretId", new StringBody(secretId));
//			multipartEntity.addPart("timestamp", new StringBody(now));
//			multipartEntity.addPart("sign", new StringBody(sign));
//			httpPost.setEntity(multipartEntity);
//			CloseableHttpResponse response = httpclient.execute(httpPost);
//			try {
//				HttpEntity entity = response.getEntity();
//				if (entity != null) {
//					responseStr = EntityUtils.toString(entity);
//				}
//			} finally {
//				response.close();
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//			throw e;
//		} finally {
//			try {
//				httpclient.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		ObjectMapper om = new ObjectMapper();
//		R r = om.readValue(responseStr, R.class);
//		try {
//			if (r.getSuccess()) {
//				FileStprageResponse result = om.convertValue(r.getContent(), FileStprageResponse.class);
//				r.setContent(result);
//			}
//		} catch (Exception e) {
//			r.setSuccess(false);
//			r.setCode(R.RCODE.SYS_ERROR.getCode());
//			r.setMessage(R.RCODE.SYS_ERROR.getMessage());
//			e.printStackTrace();
//		}
//		return r;
//	}

	/**
	 * 保费计算
	 * @param policy
	 * @param config
	 * @return
	 * @throws IOException
	 */
	public static R calculate(PolicyRequest policy, ApiConfig config) throws IOException {
		ObjectMapper om = new ObjectMapper();
		om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		String response = HttpPostUtil.send(om.writeValueAsString(policy), config, false);
		R r = om.readValue(response, R.class);
		try {
			if (r.getSuccess()) {
				PolicyCalculateVo result = om.convertValue(r.getContent(), PolicyCalculateVo.class);
				r.setContent(result);
			}
		} catch (Exception e) {
			r.setSuccess(false);

			r.setCode(R.RCODE.SYS_ERROR.getCode());
			r.setMessage(R.RCODE.SYS_ERROR.getMessage());
			e.printStackTrace();
		}
		return r;
	}
}
